//
//  WWSameSickModel.m
//  快医
//
//  Created by 孙博 on 16/7/7.
//
//

#import "WWSameSickModel.h"
#import "XHRNetworkingToolManager.h"

#define NUM @"number"
#define TITLE @"title"
#define TAPED @"taped"

/** 选择并发症列表接口 */
#define kComplicationListUrl @"http://iosapi.itcast.cn/complicationList.json.php"

@implementation WWSameSickModel 

+ (instancetype)modelWithDict:(NSDictionary *)dict{
    
    WWSameSickModel *model = [WWSameSickModel new];
    
    [model setValuesForKeysWithDictionary:dict];
    
    return model;
}

/**
 *  本地拉取数据
 */
+ (NSArray<WWSameSickModel *> *)synSicks{
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"complica.plist" ofType:nil];
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray *mArr = [NSMutableArray array];
    
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        WWSameSickModel *model= [WWSameSickModel modelWithDict:obj];
        
        [mArr addObject:model];
    }];
    
    return mArr.copy;
}


- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}

+ (void)getSynSicksSuccess:(void (^)(NSArray<WWSameSickModel *> *))successBlock error:(void (^)())errorBlock{
    
    XHRNetworkingToolManager *manager = [XHRNetworkingToolManager shareManager];
    
    // 设置为通用格式
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    NSString *urlString = kComplicationListUrl;
    NSDictionary *paramDic = @{@"page":@1,@"page_size":@15,@"ci2_id":@3
                               };
    
    [manager POST:urlString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        NSMutableArray *mArr = [NSMutableArray array];
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           // 字典转模型
            WWSameSickModel *model = [WWSameSickModel modelWithDict:obj];

            [mArr addObject:model];
            
        }];
        NSLog(@"%@",mArr.copy);
        if (successBlock) {
            successBlock(mArr.copy);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (errorBlock) {
            errorBlock();
        }
    }];
}

- (NSString *)description{
    
    return [NSString stringWithFormat:@"%@,%@,%d,taped: %d",[super description],self.title,self.number,self.taped];
}

#pragma
#pragma mark -归档解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    // 对每一项属性进行解档
    if (self = [super init]) {
        _title = [aDecoder decodeObjectForKey:TITLE];
        _number = [aDecoder decodeIntForKey:NUM];
        _taped = [aDecoder decodeBoolForKey:TAPED];
    }
    return self;
}

// 归档
- (void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:_title forKey:TITLE];
    [aCoder encodeInt:_number forKey:NUM];
    [aCoder encodeBool:_taped forKey:TAPED];
}

@end
